library(tidyverse)
tables = list.files('output/tables', full.names = F)
tables = tables[tables!='slides']
relabel = function(t) {
  tex = read_lines(paste0('output/tables/', t))
  label_line = which(str_detect(tex, fixed('label{')))
  label = str_remove(tex[label_line], fixed('\\label{'))
  label = str_remove(label, fixed('}'))
  label = str_remove(label, ' ')
  label = gsub(" ", "", label, fixed = TRUE)
  caption_line = which(str_detect(tex, fixed('caption')))
  tex[caption_line] = str_replace(tex[caption_line], fixed('\\caption{'), fixed(str_interp('\\caption*{Table \\ref{${label}}: ')))
  tex[label_line] = str_replace(tex[label_line], fixed('\\label{t'), '\\label{rt')
  write_lines(tex, paste0('output/response-tables/', t))
}
for (t in tables) {
  relabel(t)
}
